package ui.home.column2.message

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import model.enums.ContentType
import model.vo.MessageListItem
import theme.EasyChatTheme
import ui.compose.NetworkImage
import ui.create_group.FriendSelectRow
import ui.home.HomeViewModel
import utils.TimeStringUtil


/**
 * @author wei kunJie
 * @date 2024/3/11 15:44:21
 * 消息列表
 */
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun MessageList(easyChatTheme: MutableState<EasyChatTheme>,modifier: Modifier, messageListItems: MutableList<MessageListItem>, viewModel: HomeViewModel) {
    val isActivityVerticalScrollbar = remember { mutableStateOf(false) }
    Box(modifier = modifier
        .onPointerEvent(PointerEventType.Enter) { isActivityVerticalScrollbar.value = true }
        .onPointerEvent(PointerEventType.Exit) { isActivityVerticalScrollbar.value = false }) {
        val state = rememberLazyListState()
        //渲染列表
        LazyColumn(modifier = Modifier.fillMaxSize(), state = state) {
            items(messageListItems) { item ->
                if (viewModel.searchText.value == "") {
                    MessageRow(message = item, selectId = viewModel.msgListItemSelectId, viewModel = viewModel, easyChatTheme = easyChatTheme)
                } else if (
                    item.title.contains(viewModel.searchText.value) ||
                    item.account.contains(viewModel.searchText.value)
                ) {
                    MessageRow(message = item, selectId = viewModel.msgListItemSelectId, viewModel = viewModel, easyChatTheme = easyChatTheme)
                }
            }
        }
        //只有当鼠标进入时才出现滚动条
        if (isActivityVerticalScrollbar.value) {
            VerticalScrollbar(
                modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight(),
                adapter = rememberScrollbarAdapter(
                    scrollState = state
                )
            )
        }
    }

}

/**
 * Item
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MessageRow(easyChatTheme: MutableState<EasyChatTheme>,message: MessageListItem, selectId: MutableState<Long>, viewModel: HomeViewModel) {
    Box(
        modifier = Modifier.background(
            color = if (selectId.value == message.id) easyChatTheme.value.mainColor
            else Color.Transparent
        )
            .fillMaxWidth()
            .height(81.dp)
            .combinedClickable(onClick = {
                viewModel.column3ContentType.value = ContentType.FriendChat
                selectId.value = message.id
                viewModel.messageContentItemList.clear()
                viewModel.messageContentItemList.addAll(message.messageContentItems)
                viewModel.chatTitle.value = message.title
                message.count = 0
            }
            ),
    ) {
        Row(
            modifier = Modifier.padding(start = 15.dp, top = 15.dp, bottom = 15.dp, end = 10.dp)
        ) {
            //头像
            NetworkImage(
                url = message.headImage,
                painterFor = { remember { BitmapPainter(it) } },
                contentDescription = "头像",
                modifier = Modifier.size(51.dp).clip(CircleShape),
                contentScale = ContentScale.Crop,
                defaultPainterFor = painterResource("image/default_head.png")
            )
            Spacer(modifier = Modifier.width(10.dp))
            //标题和内容
            Column(modifier = Modifier.weight(1f).fillMaxHeight()) {
                Text(
                    text = message.title,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    style = TextStyle(
                        color = Color.Black,
                        fontSize = 17.sp
                    )
                )
                Spacer(modifier = Modifier.weight(1f))
                Text(
                    text = message.msg,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    style = TextStyle(
                        color = Color(0xFF6F6F6F),
                        fontSize = 14.5.sp
                    )
                )
            }
            //时间和未读数
            Column(horizontalAlignment = Alignment.End, modifier = Modifier.fillMaxHeight()) {
                Text(
                    text = TimeStringUtil.getTimeString(message.time),
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    style = TextStyle(
                        color = Color(0xFF6F6F6F),
                        fontSize = 13.sp
                    )
                )
                Spacer(modifier = Modifier.weight(1f))
                if (message.count > 0) {
                    Box(
                        modifier = Modifier.background(color = Color.Red, shape = RoundedCornerShape(9.dp))
                            .height(15.dp)
                            .padding(horizontal = 4.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = if (message.count < 100) message.count.toString() else "99",
                            style = TextStyle(
//                            color = Color(0xFF6F6F6F),
                                color = Color.White,
                                fontSize = 13.sp,
                                textAlign = TextAlign.Center
                            ),

                            )
                    }
                }
            }
        }
    }
}
